﻿#include "GameCore.h"
void Convey_floor(int newcell)
{
	int op = newcell;
	switch (op)
	{
	case 9:    //9是上楼
		if (1)
		{
			FindRole();
			level++;
			if (level == 1)
			{
				Gamemap[1][1][5] = 2;
				row = 1;
				col = 5;
				Draw();
			}
			else if (level == 2)
			{
				Gamemap[2][11][5] = 2;
				row = 11;
				col = 5;
				Draw();
			}
			else if (level == 3)
			{
				Gamemap[3][1][2] = 2;
				row = 1;
				col = 2;
				Draw();
			}
			else if (level == 4)
			{
				Gamemap[4][10][8] = 2;
				row = 10;
				col = 8;
				Draw();
			}
			else if (level == 5)
			{
				Gamemap[5][1][5] = 2;
				row = 1;
				col = 5;
				Draw();
			}
			return;
		}
		else
		{
			return;
		}
		break;
	case 10:   //10是下楼
		if (1)
		{
			FindRole();
			level--;
			if (level == 0)
			{
				Gamemap[0][1][6] = 2;
				row = 1;
				col = 6;
				Draw();
			}
			else if (level == 1)
			{
				Gamemap[1][11][5] = 2;
				row = 11;
				col = 5;
				Draw();
			}
			else if (level == 2)
			{
				Gamemap[2][1][2] = 2;
				row = 1;
				col = 2;
				Draw();
			}
			else if (level == 3)
			{
				Gamemap[3][10][8] = 2;
				row = 10;
				col = 8;
				Draw();
			}
			else if (level == 4)
			{
				Gamemap[4][1][6] = 2;
				row = 1;
				col = 6;
				Draw();
			}
			return;
		}
		else
		{
			return;
		}
		break;
	}
}
void FindRole() {
	for (int i = 0; i < 14; i++) {
		for (int j = 0; j < 14; j++) {
			if (Gamemap[level][i][j] == 16) {	//遍历数组找到角色位置
				row = i;	//更新当前行位置
				col = j;	//更新当前列位置
				return;
			}
		}
	}
}

void Draw() {
	BeginBatchDraw();// 开始批量绘图模式，避免画面闪烁
	cleardevice();
	TCHAR bgPath[MAX_PATH];// 定义一个字符数组，用于存储背景图片的路径
	_stprintf_s(bgPath, _T("assets/picture/map%d.png"), level + 1);// 格式化背景图片的路径，根据当前关卡 level 来确定具体的图片文件名
	DWORD fileAttr = GetFileAttributes(bgPath);
	if (fileAttr != INVALID_FILE_ATTRIBUTES && !(fileAttr & FILE_ATTRIBUTE_DIRECTORY)) {
		loadimage(&bgimage, bgPath, windowWidth, windowHeight, true);
		cleardevice();
		putimage(0, 0, &bgimage);
	}
	const int mapStartX = 232;
	const int mapStartY = 32;
	for (int i = 0; i < 14; i++) {	// 遍历地图的行 列
		for (int j = 0; j < 14; j++) {
			int tileID = Gamemap[level][i][j];
			int drawX = mapStartX + j * 32;
			int drawY = mapStartY + i * 32;
			transparentimage3(NULL, drawX, drawY, &img[tileID]);
		}
	}
	loadimage(&icon, "assets/picture/manual_icon.png");
	settextstyle(28, 0, _T("站酷庆科黄油体常规"));
	settextcolor(BROWN);
	setbkmode(TRANSPARENT);
	outtextxy(110, 103, TurnChar(roleman.hp));
	outtextxy(110, 158, TurnChar(roleman.att));
	outtextxy(110, 212, TurnChar(roleman.def));
	outtextxy(55, 290, TurnChar(roleman.watch));
	outtextxy(118, 290, TurnChar(roleman.lament));
	outtextxy(177, 290, TurnChar(roleman.lotus));
	outtextxy(55, 322, TurnChar(roleman.rkey));
	outtextxy(118, 322, TurnChar(roleman.ykey));
	outtextxy(177, 322, TurnChar(roleman.bkey));
	outtextxy(55, 352, TurnChar(roleman.money));
	transparentimage3(NULL, 130, 352, &icon);
	outtextxy(158, 356, "[Q]");
	EndBatchDraw();	// 结束批量绘图模式，将所有绘制的内容一次性显示在窗口中
}
void DrawEnd() {
	BeginBatchDraw();
	cleardevice();
	ifstream file_check("assets/picture/map.png");
	if (file_check) {
		file_check.close();
		loadimage(&bgimage, _T("assets/picture/map.png"), windowWidth, windowHeight, false);
		putimage(0, 0, &bgimage);
	}
	const int mapStartX = 232;
	const int mapStartY = 32;
	for (int i = 0; i < 14; i++) {
		for (int j = 0; j < 14; j++) {
			int tileID = Gamemap[level][i][j];
			int drawX = mapStartX + j * 32;
			int drawY = mapStartY + i * 32;
			transparentimage3(NULL, drawX, drawY, &img[tileID]);
		}
	}
	loadimage(&icon, "assets/picture/manual_icon.png");
	settextstyle(28, 0, _T("站酷庆科黄油体常规"));
	settextcolor(BROWN);
	setbkmode(TRANSPARENT);
	outtextxy(110, 103, TurnChar(roleman.hp));
	outtextxy(110, 158, TurnChar(roleman.att));
	outtextxy(110, 212, TurnChar(roleman.def));
	outtextxy(55, 290, TurnChar(roleman.watch));
	outtextxy(118, 290, TurnChar(roleman.lament));
	outtextxy(177, 290, TurnChar(roleman.lotus));
	outtextxy(55, 322, TurnChar(roleman.rkey));
	outtextxy(118, 322, TurnChar(roleman.ykey));
	outtextxy(177, 322, TurnChar(roleman.bkey));
	outtextxy(55, 352, TurnChar(roleman.money));
	transparentimage3(NULL, 130, 352, &icon);
	outtextxy(158, 356, "[Q]");
	EndBatchDraw();
}
void GameDetect() {
	ExMessage msg;
	peekmessage(&msg, EX_KEY, 0);
	if (msg.message == WM_KEYDOWN) {
		int newRow = row, newCol = col;
		char input = msg.vkcode;
		switch (input) {
		case 'W':	// 如果按下的是 'W' 键，角色向上移动
			AutoSound();
			newRow--;
			break;
		case 'S':	// 如果按下的是 'S' 键，角色向下移动
			AutoSound();
			newRow++;
			break;
		case 'A':	// 如果按下的是 'A' 键，角色向左移动
			AutoSound();
			newCol--;
			break;
		case 'D':	// 如果按下的是 'D' 键，角色向右移动
			AutoSound();
			newCol++;
			break;
		default:	// 如果按下的不是上述四个键，直接返回，不做处理
			return;
		}
		int newCell = Gamemap[level][newRow][newCol];	// 获取角色移动到的新位置的地图单元格的值
		if (newCell == 0) {
			Gamemap[level][row][col] = 0;	// 将角色原来所在的位置置为空地
			row = newRow;	// 更新角色的行坐标为新的行列坐标
			col = newCol;
			Gamemap[level][row][col] = 2;
		}
		else if (newCell == 1) {	// 如果新位置是墙 不做处理

		}
		else if (newCell == 3) {
			roleman.ykey++;	// 角色的黄色钥匙数量加1
			Gamemap[level][newRow][newCol] = 0;	// 将该位置置为空地
		}
		else if (newCell == 4) {
			roleman.bkey++;
			Gamemap[level][newRow][newCol] = 0;
		}
		else if (newCell == 5) {
			roleman.rkey++;
			Gamemap[level][newRow][newCol] = 0;
		}
		else if (newCell == 6) {
			if (roleman.ykey > 0) {
				roleman.ykey--;
				Gamemap[level][newRow][newCol] = 0;
			}
		}
		else if (newCell == 7) {
			if (roleman.bkey > 0) {
				roleman.bkey--;
				Gamemap[level][newRow][newCol] = 0;
			}
		}
		else if (newCell == 8) {
			if (roleman.rkey > 0) {
				roleman.rkey--;
				Gamemap[level][newRow][newCol] = 0;
			}
		}
		else if (newCell == 9 || newCell == 10) {	// 如果新位置是传送点
			Gamemap[level][row][col] = 0;	// 将角色原来所在的位置置为空地
			Convey_floor(newCell);	// 调用传送函数进行传送
		}

		else if (newCell >= 11 && newCell <= 200) {	// 如果新位置是怪物
			nowMonster = newCell-11;
			if (Battle(&roleman, &monsters[newCell - 11])) {	// 进行战斗，如果战斗胜利
				Gamemap[level][newRow][newCol] = 0;	// 将该位置置为空地
			}
		}
		else if (newCell == 201) {	// 如果新位置是商店
			initShopView(shop, roleman);	// 初始化商店界面
		}
		else if (newCell == 301) {
			roleman.watch++;
			Gamemap[level][newRow][newCol] = 0;
		}
		else if (newCell == 302) {
			roleman.lotus++;
			Gamemap[level][newRow][newCol] = 0;
		}
		else if (newCell == 303) {
			roleman.lament++;
			Gamemap[level][newRow][newCol] = 0;
		}
		else if (newCell == 304) {
			roleman.hp += 100;
			Gamemap[level][newRow][newCol] = 0;
		}
		else if (newCell == 305) {
			roleman.att += 5;
			Gamemap[level][newRow][newCol] = 0;
		}
		else if (newCell == 306) {
			roleman.def += 5;
			Gamemap[level][newRow][newCol] = 0;
		}
		else if (newCell == 401 || newCell == 402) {	// 如果新位置是BOSS
			nowMonster = 27;
			row = newRow;	// 更新角色的行列坐标为新的行坐标
			col = newCol;
			GameSettlement();	// 进行游戏结算
			Sleep(200);// 暂停200毫秒展示结算界面
		}
	}
}